#include"ft3x68_core.h"
#include "printf.h"

/*****************************************************************************
 *	Name: get_flow_cnt
 *	Brief: Read flow cnt(0x91)
 *	Input:
 *	Output:
 *	Return:  1(true) - Reg 0x91(flow cnt) abnormal: hold a value for 5 times
 *			 0(false) - Reg 0x91(flow cnt) normal
 *****************************************************************************/
 u8 get_flow_cnt(void)
 {
	 int	 ret = 0;
	 u8 	 reg_value = 0;
	 u8 	 reg_addr = 0;
	 reg_addr = FTS_REG_FLOW_WORK_CNT;
	 #ifdef FT3X68_Debug_Printf
     printf("[FTS]---get_flow_cnt----\r\n");
	 #endif
	 ret = fts_read(FT3168_DEVICE_I2C_ADDRESS,&reg_addr,1,&reg_value,1);
	 if (ret < 0) {
	 	 #ifdef FT3X68_Debug_Printf
		 printf("[FTS]read reg0x91 fail,ret:%d\r\n", ret);
		 #endif
		 fts_esdcheck_data.nack_cnt++;
	 } else {
		 if ( reg_value == fts_esdcheck_data.flow_work_cnt_last ) {
			 fts_esdcheck_data.flow_work_hold_cnt++;
			 #ifdef FT3X68_Debug_Printf
			 printf("[FTS]reg0x91,val:%x,last:%x,holdcnt:%d\r\n", reg_value,fts_esdcheck_data.flow_work_cnt_last,fts_esdcheck_data.flow_work_hold_cnt);
			 #endif
		 } else {
			 fts_esdcheck_data.flow_work_hold_cnt = 0;
		 }
 
		 fts_esdcheck_data.flow_work_cnt_last = reg_value;
	 }

	 /* Flow Work Cnt keep a value for 5 times, need execute TP reset */
	 if (fts_esdcheck_data.flow_work_hold_cnt >= 5) {
	 	 fts_esdcheck_data.hardware_reset_cnt++;
		 fts_esdcheck_data.flow_work_hold_cnt = 0;
		 printf("[FTS]reg0x91 keep a value for 5 times, need execute TP reset\r\n");
		 return true;
	 }
 
	 return false;
 }


int ft3x68_esdcheck_algorithm(void)
{
    int    ret = 0;
    u8     reg_value = 0;
    u8     reg_addr = 0;
    u8     hardware_reset = 0;

	 #ifdef FT3X68_Debug_Printf
     printf("[FTS]---ft3x68_esdcheck_algorithm----\r\n");
	 #endif
    /* 1. check power state, if suspend, no need check esd */
    if (fts_esdcheck_data.suspend == 1) {
		#ifdef FT3X68_Debug_Printf
        printf("[FTS]In suspend, not check esd\r\n");
		#endif 
        return 0;
    }
    /* 2. In factory mode, can't check esd */
    reg_addr = FTS_REG_WORKMODE;
    ret = fts_read(FT3168_DEVICE_I2C_ADDRESS,&reg_addr,1,&reg_value,1);
    if ( ret < 0 ) {
        fts_esdcheck_data.nack_cnt++;
    } else if ( (reg_value & 0x70) !=  FTS_REG_WORKMODE_WORK_VALUE) {
        #ifdef FT3X68_Debug_Printf
        printf("[FTS]not in work mode(%x), no check esd\r\n", reg_value);
		 #endif
        return 0;
    }
 
    /* 3. get Flow work cnt: 0x91 If no change for 5 times, then ESD and reset */
    if (!hardware_reset) {
        hardware_reset = get_flow_cnt();
    }
     #ifdef FT3X68_Debug_Printf
     printf("[FTS] after executing get_flow_cnt() ,hardware_reset=%d\r\n",hardware_reset);
	 #endif
    /* 4. If need hardware reset, then handle it here */
    if (hardware_reset == 1) {
		#ifdef FT3X68_Debug_Printf
        printf("[FTS]NoACK=%d, hardware_reset_cnt=%d\r\n",fts_esdcheck_data.nack_cnt,fts_esdcheck_data.hardware_reset_cnt);
		#endif
        ft3x68_hardware_reset();
		fts_esdcheck_data.flow_work_hold_cnt = 0;
    }
    return 0;
}
  
